<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as image;
use PhpParser\Node\Expr\Cast\Object_;

class CommonController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth.admin:admin');
    }

    public function iconfont(){
        $index=Input::get('index');
        return view('admin.tpl.iconfont',compact('index'));
    }

    public function upload(Request $request){
        if($request->isMethod('post')){
            $file=$request -> file('file');
            $basePath=Input::get('type');
            $w=Input::get('width')?Input::get('width'):config('system.thumb_width');
            $h=Input::get('height')?Input::get('height'):config('system.thumb_height');
            if($file -> isValid()){
                $entension = $file -> getClientOriginalExtension(); //上传文件的后缀.
                $newName = date('YmdHis').mt_rand(100,999).'.'.$entension;
                $file -> move(public_path().'/uploads/'.$basePath.'/'.date('Ymd'),$newName);
                $filepath = 'uploads/'.$basePath.'/'.date('Ymd').'/'.$newName;
                $image=Image::make(public_path().'/uploads/'.$basePath.'/'.date('Ymd').'/'.$newName);
                if(config('system.crop')){
                    if($image->height()>$image->width()){
                        $image->resize($w,null, function ($constraint) {
                            $constraint->aspectRatio();
                        });

                        $xx=($image->height()-$h)/2;
                        if($xx<0){
                            $xx=0;
                        }
                        $image->crop($w, $h, 0, $xx);
                    }else{
                        $image->resize(null,$h, function ($constraint) {
                            $constraint->aspectRatio();
                        });
                        $xx=($image->width()-$w)/2;
                        if($xx<0){
                            $xx=0;
                        }
                        $image->crop($w, $h, $xx, 0);
                    }
                }else{
                    $image->resize($w,$h);
                }
                if(config('system.watermark')){
                    //添加水印
                    $image->insert(public_path().'/public/images/water.png', 'bottom-right', 10, 10);
                }
                $image->save(public_path().'/uploads/'.$basePath.'/'.date('Ymd').'/thumb_'.$w.'_'.$h.'_'.$newName);

                $ret=[
                    'status' => 200,
                    'code' => 0,
                    "msg"=>trans('config.submit_success'),
                    "data"=>[
                        'src'=>$filepath,
                        'thumb'=>'uploads/'.$basePath.'/'.date('Ymd').'/thumb_'.$w.'_'.$h.'_'.$newName,
                    ]
                ];
                return $ret;
            }else{
                $ret=[
                    'status' => 300,
                    'code' => 'error',
                    'msg'=>trans('config.submit_error'),
                ];
                return $ret;
            }
            return $ret;
        }
    }

    public function cache(){
        $filePath = storage_path("framework/views");
        $file=scandir($filePath);
        foreach($file as $v){
            if($v!="." && $v!=".." && $v!=".gitignore"){
                unlink(storage_path("framework/views/".$v));
            }
        }
        return "Clear Views Cache Success!!";
    }
}
